<?php
namespace app\index\controller;

use think\Request;
use think\Db;

use app\index\model\appKind as AppKindModel;

class AppKind 
{
    public function getAllAppKind(Request $request)
    {
        //无参请求

        //初始化消息
        $message = '';
        $status = 0;
        
        //获取所有的数据
        $allNote = AppKindModel::all();

        if ($allNote == NULL) {
            $message = 'the app kind is empty.';
            $status = 0;
        }
        else {
            $message = 'get app kind success.';
            $status = 0;
        }

        //返回消息
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, $allNote);
        return $json;

    }
    public function getAppKind(Request $request)
    {
        //获取请求参数
        $dataNum = $request->param('dataNum');
        $page = $request->param('page');
        //根据页码和数据数目获取应查询数据的第一个 id     
        $boderId = 0;
        if ($page == 1) {
            $boderId = 0;
        }
        else {

            $allNotDataId = AppKindModel::all(function ($query) use ($page, $dataNum) {
                $query->limit($dataNum * ($page - 1));
            });
            $boderId = $allNotDataId[count($allNotDataId) - 1]['id'];
            
        }
        
        //初始化消息
        $message = '';
        $status = 0;
       
        //获取数据
        $appKind = AppKindModel::all(function ($query) use ($boderId, $dataNum) {
            $query->where('id', '>', $boderId)->limit($dataNum);
        });
        //读取表中数据条数
        $table = "dcw_fastask_kind";
        $sql = "SELECT COUNT(*) AS dataSum FROM $table";
        $res = Db::query($sql);
        $dataSum = $res[0]['dataSum'];

        if ($appKind == NULL) {
            $message = 'app kind is empty.';
            $status = 0;
        }
        else {
            $message = 'get app kind success.';
            $status = 0;
        }

        //返回数据
        $data = [];
        $data['kindDatas'] = $appKind;
        $data['dataSum'] = $dataSum;
        $data['borderId'] = $boderId;
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, $data);
        return $json;

    }

    public function addAppKind(Request $request)
    {
        //获取请求参数
        $kindName = $request->param('kindName');
        $kindLogoUrl = $request->param('kindLogoUrl');
        $kindAskPrice = $request->param('kindAskPrice');
        $kindSolution = $request->param('kindSolution');
        $kindAdvantage = $request->param('kindAdvantage');
        $isShowPage = $request->param('isShowPage');
        $isShow = $request->param('isShow');

        //初始化消息
        $message = '';
        $status = 0;

        //添加数据
        $newAppKind = new AppKindModel;
        $newAppKind->kindName = $kindName;
        $newAppKind->kindLogoUrl = $kindLogoUrl;
        $newAppKind->kindAskPrice = $kindAskPrice;
        $newAppKind->kindSolution = $kindSolution;
        $newAppKind->kindAdvantage = $kindAdvantage;
        $newAppKind->isShowPage = $isShowPage;
        $newAppKind->isShow = $isShow;
        
        if ($newAppKind->save()) {
            $message = 'add app kind success.';
            $status = 0;
        }
        else {
            $message = 'add app kind fail.';
            $status = 1;
        }

        //返回数据
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, array());
        return $json;

    }
    public function editAppKind(Request $request)
    {
        //获取请求参数
        $id = $request->param('id');
        $kindName = $request->param('kindName');
        $kindLogoUrl = $request->param('kindLogoUrl');
        $kindAskPrice = $request->param('kindAskPrice');
        $kindSolution = $request->param('kindSolution');
        $kindAdvantage = $request->param('kindAdvantage');
        $isShowPage = $request->param('isShowPage');
        $isShow = $request->param('isShow');

        //初始化消息
        $message = '';
        $status = 0;

        $oldAppKind = AppKindModel::get($id);
        $oldAppKind->kindName = $kindName;
        $oldAppKind->kindLogoUrl = $kindLogoUrl;
        $oldAppKind->kindAskPrice = $kindAskPrice;
        $oldAppKind->kindSolution = $kindSolution;
        $oldAppKind->kindAdvantage = $kindAdvantage;
        $oldAppKind->isShowPage = $isShowPage;
        $oldAppKind->isShow = $isShow;

        if ($oldAppKind->save()) {
            $message = 'update app kind success.';
            $status = 0;
        }
        else {
            $message = 'update app kind fail.';
            $status = 1;
        }

        //返回消息
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, array());
        return $json;
    }
    
    public function delAppKind(Request $request)
    {
        //获取请求参数
        $id = $request->param('id');

        //初始化消息
        $message = '';
        $status = 0;

        //删除数据
        if (AppKindModel::destroy($id)) {
            $message = 'del app kind success.';
            $status = 0;
        }
        else {
            $message = 'del app kind fail.';
            $status = 1;
        }

        //返回消息
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, array());
        return $json;
                
    }
    
    public function delAllAppKind(Request $request)
    {
        //无参请求

        //初始化消息
        $message = '';
        $status = 0;

        //删除数据
        if (AppKindModel::where('id', '>', 0)->delete()) {
            $message = 'del all app kind success.';
            $status = 0;
        }
        else {
            $message = 'del all app kind fail.';
            $status = 1;
        }

        //返回消息
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, array());
        return $json;
    }
}